layui.use(['layer', 'form', 'laydate'], function() {

    var layer = layui.layer
        ,form = layui.form
        ,laydate = layui.laydate;

    laydate.render({
        elem: '.startTime'
        ,type: 'datetime'
        ,format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
    });


    $(function() {
        form.on('submit(formDemo)', function (data) {
            var uId = data.field.uId
                ,pName = data.field.pName
                ,pBegintime = data.field.pBegintime
                ,pDuration = data.field.pDuration
                ,single = data.field.singleQuestion
                ,multiple = data.field.multipleQuestion
                ,short = data.field.shortQuestion
                ,reg = /^[0-9]\d*$/;
            if (single == ""){
                layer.tips('不能为空', '.singleQuestion', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
            if (multiple == ""){
                layer.tips('不能为空', '.multipleQuestion', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
            if (short == ""){
                layer.tips('不能为空', '.shortQuestion', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
            if (pBegintime == ""){
                layer.tips('开考时间不能为空', '.startTime', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            } else {
                var now = new Date().getTime()
                    ,begintime = new Date(pBegintime).getTime();
                if (begintime <= now){
                    layer.tips('开考时间不能设置在以前', '.startTime', {
                        tips: [1, '#3595CC'],
                        time: 4000
                    });
                    return false;
                }
            }
            if (!reg.test(pDuration) || pDuration <= 0){
                layer.tips('请输入正确的考试时长', '.pDuration', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }
            $.ajax({
                url: '/paper/insertPaper',
                type: 'post',
                data: {
                    uId : uId,
                    pName : pName,
                    pBegintime : pBegintime,
                    pDuration : pDuration,
                    qSingle : single,
                    qMultiple : multiple,
                    qShort : short
                },
                success: function (res) {
                    if (res.code == 200){
                        //试卷添加成功
                        layer.msg(res.msg);
                        setTimeout(function () {
                            //调用父页面的方法
                            parent.tools.reloadTable();
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }, 1000);
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });
    });


});

$(".singleQuestion").blur(function () {
    var single = $(".singleQuestion");
    var single_msg = $(".single_msg");
    regexNum(single,single_msg,'1','单选');
});

$(".multipleQuestion").blur(function () {
    var multiple = $(".multipleQuestion");
    var multiple_msg = $(".multiple_msg");
    regexNum(multiple,multiple_msg,'2','多选');
});

$(".shortQuestion").blur(function () {
    var short = $(".shortQuestion");
    var short_msg = $(".short_msg");
    regexNum(short,short_msg,'3','简答');
});

$("#close").click(function () {
    //调用父页面的方法
    parent.tools.reloadTable();
    //当你在iframe页面关闭自身时
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
});

//判断数据是否为整数
function regexNum(obj, msg,type,qType) {
    var reg = /^[0-9]\d*$/;
    if (!reg.test(obj.val())){
        msg.html("请输入正确的数字").show();
    } else {
        // console.log(reg.test(single));
        msg.hide();
        //数据传入后台，查询该类型的题目是否有这么多
        $.get("/paper/selectQuestionCount/" + type,function (res) {
            if (res.data < obj.val()){
                obj.val(res.data);
                layer.msg('可使用的' + qType + '题只有' + res.data + '题');
            }
        })
    }
}